package v3.chapter6.singleton_5;

public class Run {
	public static void main(String[] args) {
		MyThread t1 = new MyThread();
		MyThread t2 = new MyThread();
		MyThread t3 = new MyThread();

		t1.start();
		t2.start();
		t3.start();
	}
}

class MyThread extends Thread {

	@Override
	public void run() {
		System.out.println(MyObject.getInstance().hashCode());
	}
}

// 使用双检测机制来解决问题
// 即保证了不需要同步代码的异步
// 又保证了单例的效果
class MyObject {
	private volatile static MyObject myObject;
	
	private MyObject() {}
	
	public static MyObject getInstance() {
		try {
			if(myObject!=null) {
				
			}else {
				Thread.sleep(3000);
				synchronized(MyObject.class) {
					if(myObject==null) {
						myObject=new MyObject();
					}
				}
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
		return myObject;
	}
}